package blockingQueue;

import java.util.concurrent.SynchronousQueue;

/**
 * @program: javase
 * @description: SynchronousQueue 同步队列，长度为0，一个线程添加，另一个线程立刻取
 * 每个插入操作必须等待另一个线程相应的删除操作
 * @author: lld
 * @create: 2021-04-18 22:21
 **/
public class SynchronousQueueTest {
    public static void main(String[] args) throws Exception {
        SynchronousQueue<String> strings = new SynchronousQueue<>();
        for (int i = 0; i < 10; i++) {
            final int num=i;
            new Thread(() -> {
                try {
                    strings.put(String.valueOf(num));
                    System.out.println("put");
                    System.out.println("strings.size() = " + strings.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
            new Thread(() -> {
                try {
                    String take = strings.take();
                    System.out.println("take = " + take);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }

    }
}
